////////// Map Event ///////////
AtmMapEvent = {
	lat: null,
	lng: null,
  showDirection: function(lat, lng) {
    var orLat = 11.533411;
    var orLng = 104.922775;
    this.lat = lat;
    this.lng = lng;
    if (AtmMap) {
        // Remove polyline
        this.removePolyline();
        if (Session.get('geo_user_location') === null) {
            this.setGeo(lat, lng); // Set your current location
        }
        var gul = Session.get('geo_user_location');
        if (gul) {
            var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
            var schoolShadow = {
                url: iconBase + 'schools_maps.shadow.png',
                anchor: new google.maps.Point(16, 34)
            };
            AtmMap.addMarker({
                lat: orLat,
                lng: orLng,
                icon: iconBase + 'schools_maps.png',
                shadow: schoolShadow,
                infoWindow: {
                    content: "<h3>Your current location.</h3>"
                }
            })
            var currentGeo = gul.toString().split('_');
            if (currentGeo.length > 0) {
               orLat = currentGeo[0];
               orLng = currentGeo[1];
            }
            AtmMapEvent.getDirection(orLat, orLng, lat, lng);
        }
    }
   },
   setGeo: function() {
        if (!navigator.geolocation){
            return false;
        }
        function success(position) {
        	var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
			var schoolShadow = {
                url: iconBase + 'schools_maps.shadow.png',
                anchor: new google.maps.Point(16, 34)
			};

			var ll = position.coords.latitude + '_' + position.coords.longitude;
			Session.set('geo_user_location', ll);
			AtmMap.addMarker({
				lat: position.coords.latitude,
				lng: position.coords.longitude,
				icon: iconBase + 'schools_maps.png',
				shadow: schoolShadow,
				infoWindow: {
					content: "<h3>Your current location.</h3>"
				}
			})
			AtmMapEvent.getDirection(position.coords.latitude, position.coords.longitude);
			console.log('get geo :' + position.coords.latitude);
		}
		function error() {
			Session.set('geo_user_location', null);
			console.log('Error get geo location');
			return false;
		}
		navigator.geolocation.getCurrentPosition(success, error);
    },
	getDirection: function(orLat, orLng) {
		jQuery('#instructions').empty();
        AtmMap.setZoom(15);
        AtmMap.setCenter(orLat, orLng);
        AtmMap.travelRoute({
            origin: [orLat, orLng],
            destination: [this.lat, this.lng],
            travelMode: 'walking',
            step: function(e) {
                jQuery('#instructions').append('<li> <i class="icon-arrow-right"></i> '+e.instructions+'</li>');
                jQuery('#instructions li:eq('+e.step_number+')').delay(450*e.step_number).fadeIn(200, function(){
                    AtmMap.setCenter(e.end_location.lat(), e.end_location.lng());
                    AtmMap.drawPolyline({
                        path: e.path,
                        strokeColor: '#00f',
                        strokeOpacity: 0.6,
                        strokeWeight: 6
                    });
                });
            }
        });
	},
    removePolyline: function() {
        for( var i=0; i < AtmMap.polylines.length; i++) {
            AtmMap.polylines[i].setMap(null);
        }
        jQuery('#instructions').empty();
    }

};